<template>
  <div class="system-log-container">
    <el-tabs v-model="activeName" @tab-click="handleTabsClick">
      <el-tab-pane label="待收账单" name="1">
        <div class="spaceForm">
          <el-form :inline="true" :model="queryForm">
            <el-form-item>
              <el-select
                v-model="queryForm.group_id"
                :clearable="true"
                placeholder="分组"
                @change="queryData"
              >
                <el-option
                  v-for="item in group"
                  :key="item.id"
                  :label="item.group_name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.date"
                end-placeholder="结束日期"
                format="yyyy-MM-dd"
                start-placeholder="开始日期"
                type="daterange"
                value-format="yyyy-MM-dd"
                @change="changeDate"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.keyword"
                clearable
                placeholder="请输入关键词"
                @keyup.enter.native="queryData"
                @clear="queryData"
              />
            </el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form>
          <el-form>
            <el-button @click="getExcel">导出表格</el-button>
          </el-form>
        </div>
        <div class="totalMoney">
          <div v-if="income != 0" class="income">
            待收
            <span>{{ income }}</span>
          </div>
          <div v-if="payed != 0" class="payed">
            待付
            <span>{{ payed }}</span>
          </div>
        </div>
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            label="费用名称"
            prop="bill_title"
            show-overflow-tooltip
          />
          <el-table-column
            label="姓名"
            prop="customer_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="联系方式"
            prop="customer_mobile"
            show-overflow-tooltip
          />
          <el-table-column
            label="地址"
            prop="room_address"
            show-overflow-tooltip
          />
          <el-table-column
            label="分组"
            prop="group_name"
            show-overflow-tooltip
          />
          <el-table-column label="金额" prop="bill_money">
            <template #default="{ row }">
              <div>+{{ row.bill_money }}元</div>
            </template>
          </el-table-column>
          <el-table-column
            label="待收日期"
            prop="bill_date"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="操作" width="120">
            <template #default="{ row }">
              <el-button type="text" @click="setPayed(row)">收款</el-button>
              <el-button type="text" @click="getPayed(row)">催缴</el-button>
              <el-button type="text" @click="deleteBill(row)">删除</el-button>
            </template>
          </el-table-column>
          <template #empty>
            <el-image
              class="vab-data-empty"
              :src="require('@/assets/empty_images/data_empty.png')"
            />
          </template>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.page"
          :layout="layout"
          :page-size="queryForm.perpage"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
        <set-payed ref="setPayed" @fetch-data="queryData" />
      </el-tab-pane>
      <el-tab-pane label="待收账单汇总" name="2">
        <div class="spaceForm">
          <el-form :inline="true" :model="queryRoomForm">
            <el-form-item>
              <el-select
                v-model="queryRoomForm.group_id"
                :clearable="true"
                placeholder="分组"
                @change="queryRoomData"
              >
                <el-option
                  v-for="item in group"
                  :key="item.id"
                  :label="item.group_name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryRoomFormDate"
                :clearable="true"
                placeholder="类型"
                @change="dateChange"
              >
                <el-option label="今日录入" value="1" />
                <el-option label="7日内录入" value="7" />
                <el-option label="30日内录入" value="30" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryRoomForm.keyword"
                clearable
                placeholder="请输入关键词"
                @keyup.enter.native="queryRoomData"
                @clear="queryRoomData"
              />
            </el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              @click="queryRoomData"
            >
              查询
            </el-button>
          </el-form>
        </div>
        <el-table v-loading="listRoomLoading" :data="roomList">
          <el-table-column
            label="房间"
            prop="room_address"
            show-overflow-tooltip
          />
          <el-table-column label="租期" min-width="210">
            <template #default="{ row }">
              <div>{{ row.start_date }}至{{ row.end_date }}</div>
            </template>
          </el-table-column>
          <el-table-column
            label="分组"
            prop="group_name"
            show-overflow-tooltip
          />
          <el-table-column label="已收">
            <template #default="{ row }">
              <div>{{ row.total_bills_payed }}笔</div>
            </template>
          </el-table-column>
          <el-table-column label="已收金额">
            <template #default="{ row }">
              <div>{{ row.total_money_payed }}元</div>
            </template>
          </el-table-column>
          <el-table-column label="待收">
            <template #default="{ row }">
              <div>{{ row.total_bills_nopayed }}笔</div>
            </template>
          </el-table-column>
          <el-table-column label="待收金额">
            <template #default="{ row }">
              <div>{{ row.total_money_nopayed }}元</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="80">
            <template #default="{ row }">
              <el-button type="text" @click="rentDetail(row)">详情</el-button>
            </template>
          </el-table-column>
          <template #empty>
            <el-image
              class="vab-data-empty"
              :src="require('@/assets/empty_images/data_empty.png')"
            />
          </template>
        </el-table>
        <el-pagination
          background
          :current-page="queryRoomForm.page"
          :layout="layout"
          :page-size="queryRoomForm.perpage"
          :total="roomTotal"
          @current-change="handleRoomCurrentChange"
          @size-change="handleRoomSizeChange"
        />
        <rent-detail ref="rentDetail" @fetch-data="queryRoomData" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { billList, deleteBill, getIncomeRoomBill } from '@/api/bill'
import { getgroup } from '@/api/group'
import { parseTime } from '@/utils/index'
import dayjs from 'dayjs'
import setPayed from '@/views/components/components/setPayed'
import rentDetail from '@/views/components/rentDetail'
export default {
  name: 'BillList',
  components: { setPayed, rentDetail },
  data() {
    return {
      activeName: '1',
      type: 'rent',
      list: [],
      group: [],
      listLoading: true,
      imageviewerList: [],
      imageViewer: false,
      dialogDescVisible: false,
      desc: '',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      income: 0,
      payed: 0,
      queryForm: {
        keyword: '',
        group_id: '',
        bill_type: 'income',
        is_pay: 0,
        page: 1,
        perpage: 20,
        start_time: '',
        end_time: '',
      },
      roomList: [],
      roomTotal: 0,
      listRoomLoading: false,
      queryRoomFormDate: null,
      queryRoomForm: {
        keyword: null,
        group_id: null,
        start_time: null,
        end_time: null,
        page: 1,
        perpage: 20,
      },
    }
  },
  created() {
    this.getGroup()
    this.fetchData()
  },
  methods: {
    handleTabsClick(tab) {
      console.log(tab.name)
      if (tab.name == '1') {
        this.queryData()
      } else if (tab.name == '2') {
        this.queryRoomData()
      }
    },
    changeDate(value) {
      if (value) {
        this.queryForm.start_time = value[0]
        this.queryForm.end_time = value[1]
      } else {
        this.queryForm.start_time = null
        this.queryForm.end_time = null
      }
      this.queryData()
    },
    handleSizeChange(val) {
      this.queryForm.perpage = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    queryData() {
      this.queryForm.page = 1
      this.fetchData()
    },
    setPayed(row) {
      this.$refs['setPayed'].showEdit('rent', row)
    },
    getPayed(row) {
      console.log('催收', row)
    },
    deleteBill(row) {
      this.$baseConfirm(
        '你确定要删除此账单吗，删除后，此账单将被永久清除',
        null,
        async () => {
          const { data } = await deleteBill({ bill_ids: String(row.id) })
          if (data.is_success) {
            this.$baseMessage('删除成功', 'success', 'vab-hey-message-success')
            this.fetchData()
          }
        }
      )
    },
    async fetchData() {
      var queryData = Object.assign({}, this.queryForm)
      this.listLoading = true
      const { data } = await billList(queryData)
      this.list = data.bill_data.items
      this.total = data.bill_data.total
      this.income = data.total_money_income
      this.payed = data.total_money_payed
      this.listLoading = false
    },
    async getGroup() {
      const { data } = await getgroup({})
      this.group = data
    },
    dateChange(e) {
      if (e) {
        this.queryRoomForm.end_time = parseTime(new Date(), '{y}-{m}-{d}')
        this.queryRoomForm.start_time = this.getDay(e)
      } else {
        this.queryRoomForm.end_time = null
        this.queryRoomForm.start_time = null
      }
      this.queryRoomData()
    },
    getDay(num) {
      return dayjs(new Date(this.queryRoomForm.end_time))
        .subtract(num, 'd')
        .format('YYYY-MM-DD')
    },
    handleRoomSizeChange(val) {
      this.queryRoomForm.perpage = val
      this.fetchRoomData()
    },
    handleRoomCurrentChange(val) {
      this.queryRoomForm.page = val
      this.fetchRoomData()
    },
    queryRoomData() {
      this.queryRoomForm.page = 1
      this.fetchRoomData()
    },
    rentDetail(row) {
      this.$refs['rentDetail'].showEdit(row)
    },
    async fetchRoomData() {
      var queryRoomData = Object.assign({}, this.queryRoomForm)
      this.listRoomLoading = true
      const { data } = await getIncomeRoomBill(queryRoomData)
      this.roomList = data.items
      this.roomTotal = data.total
      this.listRoomLoading = false
    },

    getExcel() {
      import('@/utils/excel').then((excel) => {
        const header = [
          'ID',
          '费用名称',
          '姓名',
          '联系方式',
          '地址',
          '分组',
          '待收金额',
          '待收时间',
        ]
        const filterVal = [
          'id',
          'bill_title',
          'customer_name',
          'customer_mobile',
          'room_address',
          'group_name',
          'bill_money',
          'bill_date',
        ]
        const list = this.list
        const data = this.formatJson(filterVal, list)
        const filename = 'bill' + new Date().valueOf()
        excel.export_json_to_excel({
          header,
          filename,
          data,
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        })
      )
    },
  },
}
</script>
<style lang="scss" scoped>
.spaceForm {
  .el-select {
    width: 130px;
  }
  .el-input {
    width: 130px;
  }
  .el-date-editor.el-input {
    width: 130px;
  }
  .daterange {
    margin-right: 10px;
    line-height: 32px;
  }
}
.totalMoney {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  span {
    margin-right: 20px;
    font-size: 20px;
  }
  .income span {
    color: #1ecb6b;
  }
  .payed span {
    color: #ee5959;
  }
}
.iconMore {
  .icon {
    font-size: 20px;
    cursor: pointer;
    margin-right: 10px;
    color: #999;
    &:hover {
      color: #91b8ff;
    }
  }
}
.payMenu {
  color: #5187f7;
  cursor: pointer;
}
</style>
